//GeoServer wfs服务查询类


class QueryGeoServer {
    constructor(options) {
        this.options = options;
        this.name = Cesium.defaultValue(options.name, "");

        // this._pageSize = Cesium.defaultValue(options.pageSize, 10); // 每页条数 
        // this._allCount = 0  //总记录数
        // this._allPage = 0   //总页数
        // this._pageIndex = 1  //当前第几页
        // this.arrIDS = [];
    }
    //========== 对外属性 ==========     
    // //每页条数
    // get pageSize() {
    //     return this._pageSize;
    // }
    // set pageSize(val) {
    //     this._pageSize = val;
    // }
    // //总记录数
    // get allCount() {
    //     return this._allCount;
    // }
    // //总页数
    // get allPage() {
    //     return this._allPage;
    // }
    // //当前第几页
    // get pageIndex() {
    //     return this._pageIndex;
    // }
    // set pageIndex(val) {
    //     this._pageIndex = val;
    //     this.showPage(val);
    // }

    // //========== 方法 ========== 
    // // 首页
    // showFirstPage() {
    //     this.showPage(1);
    // }
    // // 上一页
    // showPretPage() {
    //     this._pageIndex = this._pageIndex - 1;
    //     if (this._pageIndex < 1)
    //         this._pageIndex = 1;

    //     this.showPage(this._pageIndex);
    // }
    // // 下一页
    // showNextPage() {
    //     this._pageIndex = this._pageIndex + 1;
    //     if (this._pageIndex > this.allPage)
    //         this._pageIndex = this.allPage;
    //     this.showPage(this._pageIndex);
    // }

    // showPage(pageIndex) {
    //     this._pageIndex = pageIndex;
    //     this._queryPageByIds(this.lastQueryOpts);
    // }


    /**  
     * 查询 (公共入口) 
     */
    query(opt) {
        this.lastQueryOpts = opt;

        // this._pageIndex = 1;
        // this.arrIDS = []; 
 
        //请求的wfs参数
        var filter = '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><And>';

        //关键字
        if (opt.text) {
            filter += ` <PropertyIsLike wildCard="*" singleChar="#" escapeChar="!"> 
                            <PropertyName>${opt.column}</PropertyName>
                            <Literal>*${opt.text}*</Literal>
                        </PropertyIsLike>`
        }

        //限定区域范围 
        if (opt.fwEntity) {
            var drawEntity = opt.fwEntity;
            var coordinates;
            if (drawEntity.polygon) {
                coordinates = zz3d.draw.attr.polygon.getCoordinates(drawEntity);
                coordinates.push(coordinates[0]);
            }
            else if (drawEntity.rectangle) {
                coordinates = zz3d.draw.attr.rectangle.getOutlineCoordinates(drawEntity);
            }
            else if (drawEntity.ellipse) {
                coordinates = zz3d.draw.attr.ellipse.getOutlineCoordinates(drawEntity);
            }

            var polygon = "";
            for (var i = 0; i < coordinates.length; i++) {
                polygon += coordinates[i][0] + "," + coordinates[i][1] + " ";
            }
            filter += `<Intersects>
                    <PropertyName>the_geom</PropertyName>
                    <gml:Polygon>
                        <gml:outerBoundaryIs>
                            <gml:LinearRing>
                                <gml:coordinates>${polygon}</gml:coordinates>
                            </gml:LinearRing>
                        </gml:outerBoundaryIs>
                    </gml:Polygon>
                </Intersects>`
        }
 
        filter += '</And></Filter>';

        var parameters = this.options.parameters;
        parameters.filter = filter;

        var that = this;
        $.ajax({
            url: this.options.url,
            type: "post",
            data: parameters,
            success: function (featureCollection) {
                that.processFeatureCollection(featureCollection, opt);
            },
            error: function (data) {
                var msg = "请求出错(" + data.status + ")：" + data.statusText
                console.log(msg);
                if (opt.error) opt.error(data, msg)
            }
        });
    }




    processFeatureCollection(featureCollection, opt) {
        var that = this;
        if (featureCollection == undefined
            || featureCollection == null
            || featureCollection.features == null
            || featureCollection.features.length == 0) {
            if (opt.success) opt.success({
                list: null,
                dataSource: null,

                count: 0,
                // allCount: that.allCount,

                // pageSize: that.pageSize,
                // allPage: that.allPage,
                // pageIndex: that.pageIndex,
            })
        }
        else {
            //剔除有问题数据 
            var featuresOK = [];
            for (var i = 0; i < featureCollection.features.length; i++) {
                var feature = featureCollection.features[i];
                if (feature == null || feature.geometry == null
                    || feature.geometry.coordinates == null || feature.geometry.coordinates.length == 0)
                    continue;

                featuresOK.push(feature);
            }
            featureCollection.features = featuresOK;

            var dataSource = Cesium.GeoJsonDataSource.load(featureCollection, {
                clampToGround: true
            });
            dataSource.then(function (dataSource) {

                var arrResult = [];
                var entities = dataSource.entities.values;
                for (var i = 0, len = entities.length; i < len; i++) {
                    var entity = entities[i];

                    //属性
                    var attr = zz3d.util.getAttrVal(entity.properties);
                    attr.name = attr[opt.column]
                    attr._entity = entity;
                    arrResult.push(attr);

                    //popup
                    if (that.options.popup) {
                        entity.popup = {
                            html: function (entity) {
                                var attr = entity.properties;
                                return zz3d.util.getPopup(that.options.popup, attr, that.name);
                            },
                            anchor: [0, -15],
                        };
                    }

                    //文字标记
                    var attrLbl = that.options.label;
                    if (attrLbl) {
                        attrLbl.text = attr.name
                        attrLbl.clampToGround = Cesium.defaultValue(attrLbl.clampToGround, true)
                        attrLbl.pixelOffset = Cesium.defaultValue(attrLbl.pixelOffset, [0, -50])
                        attrLbl = zz3d.draw.util.getDefStyle('label', attrLbl)//默认值

                        //entity属性 
                        entity.label = zz3d.draw.attr.label.style2Entity(attrLbl);
                    }
                }

                if (opt.success) opt.success({
                    list: arrResult,
                    dataSource: dataSource,

                    count: arrResult.length,
                    // allCount: that.allCount,

                    // pageSize: that.pageSize,
                    // allPage: that.allPage,
                    // pageIndex: that.pageIndex,
                })

            }).catch(function (error) {
                if (opt.error) opt.error(error, error.message)
            });
        }
    }


}

function isObject(obj) {
    return (typeof obj == 'object') && obj.constructor == Object;
}